package org.easy.eao.spring.hibernate;

import java.sql.SQLException;
import java.util.Set;

import org.easy.eao.annotations.Persist;
import org.easy.eao.utils.ActionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * 持久化动作实现
 * 
 * @author Frank
 */
public class PersistAction extends HibernateActionSupport<Persist> {

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object execute(final Object[] parameters) {
		return getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Set<Object> entities = ActionUtils.getAllObjects(parameters);
				for (Object entity : entities)
					session.save(entity);
				return null;
			}
		});
	}

}
